package com.shop.mms_client.utils;

import android.content.Context;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import retrofit2.HttpException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public class ErrorHandler {
    
    public static void handleApiError(Context context, Throwable error) {
        String message;
        if (error instanceof HttpException) {
            message = "服务器错误，请稍后重试";
        } else if (error instanceof SocketTimeoutException) {
            message = "连接超时，请检查网络";
        } else if (error instanceof UnknownHostException) {
            message = "无法连接到服务器，请检查网络";
        } else {
            message = "发生错误: " + error.getMessage();
        }
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    public static void showError(View view, String message, String actionText, 
                               View.OnClickListener action) {
        Snackbar.make(view, message, Snackbar.LENGTH_LONG)
            .setAction(actionText, action)
            .show();
    }

    public static void showError(View view, String message) {
        Snackbar.make(view, message, Snackbar.LENGTH_SHORT).show();
    }
} 